package com.macro.mall.product.controller;

import com.google.api.client.util.Maps;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.product.dto.PmServerSpecDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
import java.util.Map;

/**
 * 属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Slf4j
@Controller
@Api(tags = "PmServerSpecController", description = "服务规格管理")
@RequestMapping("/serverspec")
public class PmServerSpecController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${host.mall.product}")
    private String HOST_MALL_PRODUCT;

    private String SERVICE_NAME="/serverspec";


    @ApiOperation(value = "服务规格列表--不分页")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmServerSpecDto>> list(@RequestParam(value = "serverSpecName", defaultValue = "") String serverSpecName,
                                                    @RequestParam(value = "isNeedPic", defaultValue = "true") boolean isNeedPic,
                                                    @RequestParam(value = "parentId", defaultValue = "") Long parentId,
                                                    @RequestParam(value = "serverSpecLevel", defaultValue = "") Integer serverSpecLevel
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/list";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("serverSpecName",serverSpecName);
        param.put("isNeedPic",isNeedPic);
        param.put("parentId",parentId);
        param.put("serverSpecLevel",serverSpecLevel);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmServerSpecController.list   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "服务规格列表--分页")
    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmServerSpecDto>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "serverSpecName", defaultValue = "") String serverSpecName,
                                                    @RequestParam(value = "isNeedPic", defaultValue = "true") boolean isNeedPic,
                                                    @RequestParam(value = "parentId") Long parentId,
                                                    @RequestParam(value = "serverSpecLevel") Integer serverSpecLevel
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/listPage";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("pageNum",pageNum);
        param.put("pageSize",pageSize);
        param.put("serverSpecName",serverSpecName);
        param.put("isNeedPic",isNeedPic);
        param.put("parentId",parentId);
        param.put("serverSpecLevel",serverSpecLevel);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmServerSpecController.listPage   restUrl="+restUrl);
        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }

    @ApiOperation(value = "保存服务规格")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@Validated @RequestBody PmServerSpecDto pmServerSpecDto, BindingResult result) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/save";
        ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(url, pmServerSpecDto, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "删除服务规格")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@RequestParam(value = "serverSpecIds") String serverSpecIds) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/delete";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("serverSpecIds",serverSpecIds);


        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmServerSpecController.delete   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "获取单个服务规格包")
    @RequestMapping(value = "/getServerSpecById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmServerSpecDto> getServerSpecById(@RequestParam(value = "serverSpecId", defaultValue = "") Long serverSpecId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getServerSpecById";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("serverSpecId",serverSpecId);


        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmServerSpecController.getServerSpecById   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
}
